#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@author:  runyuanye
@contact: yerunyuan@163.com
"""

import argparse
import os
import sys

sys.path.append('.')

"""
   将文件列表转换为带faceid的文件列表
"""


def read_file_list(file_list):
    files = []
    file_count = 0
    with open(file_list, 'r') as file:
        for line in file.readlines():
            file_count += 1
            if file_count % 10000 == 0:
                print('Read File Count: {}'.format(file_count))

            # if file_count == 10000:
            #     break

            line = line.strip()
            info = line.split('/')
            device_id = info[-4]
            video_name = info[-2]
            file_name = '{} {} {} 0'.format(device_id, video_name, line)

            files.append(file_name)

        print('Read File Count: {}'.format(file_count))
    return files


def main():
    parser = argparse.ArgumentParser(description="To FaceID File List")
    parser.add_argument(
        "--file_list", default="/rootfs/media/kasim/DataSet/FaceCropImage/Feature.select.txt", help="path to image file list", type=str
    )
    parser.add_argument(
        "--output_dir", default="/rootfs/media/kasim/DataSet/FaceCropImage", help="path to output", type=str
    )
    args = parser.parse_args()

    output_dir = args.output_dir
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
        os.system('chmod a+wr {}'.format(output_dir))

    files = read_file_list(args.file_list)

    out_file_path = os.path.join(output_dir, os.path.splitext(args.file_list)[0]+'.FaceID.txt')
    with open(out_file_path, 'w') as file:
        for file_name in files:
            file.write(file_name + '\n')

    os.system('chmod a+wr {}'.format(out_file_path))

    print('finish!')


if __name__ == '__main__':
    main()
